<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 6.2.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/avatar.jpg">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/avatar.jpg">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/avatar.jpg">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">

<link rel="stylesheet" href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&display=swap&subset=latin,latin-ext">
<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">
  <link rel="stylesheet" href="/lib/pace/pace-theme-big-counter.min.css">
  <script src="/lib/pace/pace.min.js"></script>

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"raphets.github.io","root":"/","scheme":"Mist","version":"7.8.0","exturl":false,"sidebar":{"position":"right","display":"hide","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":true,"show_result":true,"style":"default"},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":false,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}}};
  </script>

  <meta name="description" content="Android开发过程中，经常要对小数点位数各种处理，这里总结一下。">
<meta property="og:type" content="article">
<meta property="og:title" content="Java 小数点位数保留的方法总结">
<meta property="og:url" content="https://raphets.github.io/archives/5478cd25.html">
<meta property="og:site_name" content="尚拙">
<meta property="og:description" content="Android开发过程中，经常要对小数点位数各种处理，这里总结一下。">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2019-05-06T09:10:46.000Z">
<meta property="article:modified_time" content="2022-05-25T02:42:58.010Z">
<meta property="article:author" content="John Doe">
<meta property="article:tag" content="Android，技术，学习，成长">
<meta name="twitter:card" content="summary">

<link rel="canonical" href="https://raphets.github.io/archives/5478cd25.html">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>Java 小数点位数保留的方法总结 | 尚拙</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">尚拙</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
      <p class="site-subtitle" itemprop="description">一个分享技术、学习成长的个人博客网站</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a>

  </li>
        <li class="menu-item menu-item-tool">

    <a href="/tool/" rel="section"><i class="fa fa-hammer fa-fw"></i>工具</a>

  </li>
        <li class="menu-item menu-item-about">

    <a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a>

  </li>
  </ul>
</nav>




</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://raphets.github.io/archives/5478cd25.html">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.jpg">
      <meta itemprop="name" content="John Doe">
      <meta itemprop="description" content="不积跬步，无以至千里；不积小流，无以成江海。">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="尚拙">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          Java 小数点位数保留的方法总结
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2019-05-06 17:10:46" itemprop="dateCreated datePublished" datetime="2019-05-06T17:10:46+08:00">2019-05-06</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="far fa-calendar-check"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2022-05-25 10:42:58" itemprop="dateModified" datetime="2022-05-25T10:42:58+08:00">2022-05-25</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/%E6%8A%80%E6%9C%AF%E6%96%87%E7%AB%A0/" itemprop="url" rel="index"><span itemprop="name">技术文章</span></a>
                </span>
                  ，
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/%E6%8A%80%E6%9C%AF%E6%96%87%E7%AB%A0/Android/" itemprop="url" rel="index"><span itemprop="name">Android</span></a>
                </span>
            </span>

          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <p>Android开发过程中，经常要对小数点位数各种处理，这里总结一下。</p>
<span id="more"></span>

<h1 id="方法一：使用DecimalFormat类"><a href="#方法一：使用DecimalFormat类" class="headerlink" title="方法一：使用DecimalFormat类"></a>方法一：使用DecimalFormat类</h1><p>举个例子，假如我们需要保留两位小数，我们可以这样写</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">DecimalFormat df = new DecimalFormat(&quot;0.00&quot;);</span><br></pre></td></tr></table></figure>
<p>测试如下：</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">double d = 0.200;</span><br><span class="line">DecimalFormat df = new DecimalFormat(&quot;0.00&quot;);</span><br><span class="line">System.out.println(df.format(d));</span><br></pre></td></tr></table></figure>
<p>输出结果为：</p>
<p>0.20</p>
<p>若double d&#x3D;0.000；输出结果为0.00；</p>
<p>若double d&#x3D;0；输出结果为0.00；</p>
<p>若double d&#x3D;41.2345；输出结果为41.23；</p>
<p>经测试，不管double d的值为多少，最后结果都是正常的两位小数。</p>
<p>同理若是保留一位小数，使用如下代码</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">DecimalFormat df = new DecimalFormat(&quot;0.0&quot;);</span><br></pre></td></tr></table></figure>

<h4 id="总结：若是这种写法DecimalFormat-df-x3D-new-DecimalFormat-“0-00”-，不管传入的任何值，均保留两位小数"><a href="#总结：若是这种写法DecimalFormat-df-x3D-new-DecimalFormat-“0-00”-，不管传入的任何值，均保留两位小数" class="headerlink" title="总结：若是这种写法DecimalFormat df &#x3D; new DecimalFormat(“0.00”)，不管传入的任何值，均保留两位小数"></a>总结：若是这种写法DecimalFormat df &#x3D; new DecimalFormat(“0.00”)，不管传入的任何值，均保留两位小数</h4><p>还有一种写法是这样：</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">double d = 41.123;</span><br><span class="line">DecimalFormat df = new DecimalFormat(&quot;#.##&quot;);</span><br><span class="line">System.out.println(df.format(d));</span><br></pre></td></tr></table></figure>
<p>输出结果为：</p>
<p>41.12</p>
<p>若double d&#x3D;2.00，输出结果为2；</p>
<p>若double d&#x3D;41.001，输出结果为41；</p>
<p>若double d&#x3D;41.010，输出结果为41.01；</p>
<p>若double d&#x3D;0，输出结果为0；</p>
<p>若double d&#x3D;0.200，输出结果为0.2；</p>
<h4 id="总结：若是这种写法DecimalFormat-df-x3D-new-DecimalFormat-“-”-，则保留小数点后面不为0的两位小数，这种写法不能保证保留2为小数，但能保证最后一位数不为0；"><a href="#总结：若是这种写法DecimalFormat-df-x3D-new-DecimalFormat-“-”-，则保留小数点后面不为0的两位小数，这种写法不能保证保留2为小数，但能保证最后一位数不为0；" class="headerlink" title="总结：若是这种写法DecimalFormat df &#x3D; new DecimalFormat(“#.##”)，则保留小数点后面不为0的两位小数，这种写法不能保证保留2为小数，但能保证最后一位数不为0；"></a>总结：若是这种写法DecimalFormat df &#x3D; new DecimalFormat(“#.##”)，则保留小数点后面不为0的两位小数，这种写法不能保证保留2为小数，但能保证最后一位数不为0；</h4><h1 id="方法二：使用String-format-方法"><a href="#方法二：使用String-format-方法" class="headerlink" title="方法二：使用String.format()方法"></a>方法二：使用String.format()方法</h1><p>测试：</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">double d = 0.6544;</span><br><span class="line">String s=String.format(&quot;%.2f&quot;,d);</span><br><span class="line">System.out.println(s);</span><br></pre></td></tr></table></figure>

<p>输出结果为：</p>
<p>0.65</p>
<p>若double d&#x3D;0.6566，输出结果为0.66；</p>
<p>若double d&#x3D;0，输出结果为0.00;</p>
<h6 id="注释：其中String-s-x3D-String-format-“-2f”-d-表示小数点后任意两位小数，其中2为表示两位小数，若需要三位小数，把2改为3即可，其他同理。"><a href="#注释：其中String-s-x3D-String-format-“-2f”-d-表示小数点后任意两位小数，其中2为表示两位小数，若需要三位小数，把2改为3即可，其他同理。" class="headerlink" title="注释：其中String s&#x3D;String.format(“%.2f”,d)表示小数点后任意两位小数，其中2为表示两位小数，若需要三位小数，把2改为3即可，其他同理。"></a>注释：其中String s&#x3D;String.format(“%.2f”,d)表示小数点后任意两位小数，其中2为表示两位小数，若需要三位小数，把2改为3即可，其他同理。</h6><h4 id="总结：这种方法不管传入的值是多少，均保留两位小数，并且符合四舍五入的规则。"><a href="#总结：这种方法不管传入的值是多少，均保留两位小数，并且符合四舍五入的规则。" class="headerlink" title="总结：这种方法不管传入的值是多少，均保留两位小数，并且符合四舍五入的规则。"></a>总结：这种方法不管传入的值是多少，均保留两位小数，并且符合四舍五入的规则。</h4><h1 id="方法三：使用BigDecimal类"><a href="#方法三：使用BigDecimal类" class="headerlink" title="方法三：使用BigDecimal类"></a>方法三：使用BigDecimal类</h1><p>测试：</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">double d = 1.000;</span><br><span class="line">BigDecimal bd=new BigDecimal(d);</span><br><span class="line">double d1=bd.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();</span><br><span class="line">System.out.println(d1);</span><br></pre></td></tr></table></figure>

<p>输入结果:1.0</p>
<p>若double d&#x3D;0，输出结果为0.0；</p>
<p>若double d&#x3D;1.999，输出结果为2.0；</p>
<p>若double d&#x3D;1.89，输出结果为1.89；</p>
<h4 id="总结：使用这种写法若小数点后均为零，则保留一位小数，并且有四舍五入的规则。"><a href="#总结：使用这种写法若小数点后均为零，则保留一位小数，并且有四舍五入的规则。" class="headerlink" title="总结：使用这种写法若小数点后均为零，则保留一位小数，并且有四舍五入的规则。"></a>总结：使用这种写法若小数点后均为零，则保留一位小数，并且有四舍五入的规则。</h4><h1 id="源码"><a href="#源码" class="headerlink" title="源码"></a>源码</h1><p>以下为Java测试类</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">import java.math.BigDecimal;</span><br><span class="line">import java.text.DecimalFormat;</span><br><span class="line"></span><br><span class="line">public class Test &#123;</span><br><span class="line">    public static void main(String[] args) &#123;</span><br><span class="line">        double d = 1.19;</span><br><span class="line">        System.out.println(formatDecimal4(d));</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public static String formatDecimal1(double d) &#123;</span><br><span class="line">        DecimalFormat df = new DecimalFormat(&quot;0.00&quot;);</span><br><span class="line">        return df.format(d);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public static String formatDecimal2(double d) &#123;</span><br><span class="line">        DecimalFormat df = new DecimalFormat(&quot;#.##&quot;);</span><br><span class="line">        return df.format(d);</span><br><span class="line">    &#125;</span><br><span class="line">    public static String formatDecimal3(double d)&#123;</span><br><span class="line">        return String.format(&quot;%.2f&quot;,d);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public static double formatDecimal4(double d)&#123;</span><br><span class="line">        BigDecimal bd=new BigDecimal(d);</span><br><span class="line">        double d1=bd.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();</span><br><span class="line">        return d1;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
    </div>

    
    
    

      <footer class="post-footer">

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/archives/b9e67ec1.html" rel="prev" title="HTTP 协议详解">
      <i class="fa fa-chevron-left"></i> HTTP 协议详解
    </a></div>
      <div class="post-nav-item">
    <a href="/archives/fbc16b6.html" rel="next" title="Java 集合排序总结">
      Java 集合排序总结 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#%E6%96%B9%E6%B3%95%E4%B8%80%EF%BC%9A%E4%BD%BF%E7%94%A8DecimalFormat%E7%B1%BB"><span class="nav-number">1.</span> <span class="nav-text">方法一：使用DecimalFormat类</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E6%80%BB%E7%BB%93%EF%BC%9A%E8%8B%A5%E6%98%AF%E8%BF%99%E7%A7%8D%E5%86%99%E6%B3%95DecimalFormat-df-x3D-new-DecimalFormat-%E2%80%9C0-00%E2%80%9D-%EF%BC%8C%E4%B8%8D%E7%AE%A1%E4%BC%A0%E5%85%A5%E7%9A%84%E4%BB%BB%E4%BD%95%E5%80%BC%EF%BC%8C%E5%9D%87%E4%BF%9D%E7%95%99%E4%B8%A4%E4%BD%8D%E5%B0%8F%E6%95%B0"><span class="nav-number">1.0.0.1.</span> <span class="nav-text">总结：若是这种写法DecimalFormat df &#x3D; new DecimalFormat(“0.00”)，不管传入的任何值，均保留两位小数</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E6%80%BB%E7%BB%93%EF%BC%9A%E8%8B%A5%E6%98%AF%E8%BF%99%E7%A7%8D%E5%86%99%E6%B3%95DecimalFormat-df-x3D-new-DecimalFormat-%E2%80%9C-%E2%80%9D-%EF%BC%8C%E5%88%99%E4%BF%9D%E7%95%99%E5%B0%8F%E6%95%B0%E7%82%B9%E5%90%8E%E9%9D%A2%E4%B8%8D%E4%B8%BA0%E7%9A%84%E4%B8%A4%E4%BD%8D%E5%B0%8F%E6%95%B0%EF%BC%8C%E8%BF%99%E7%A7%8D%E5%86%99%E6%B3%95%E4%B8%8D%E8%83%BD%E4%BF%9D%E8%AF%81%E4%BF%9D%E7%95%992%E4%B8%BA%E5%B0%8F%E6%95%B0%EF%BC%8C%E4%BD%86%E8%83%BD%E4%BF%9D%E8%AF%81%E6%9C%80%E5%90%8E%E4%B8%80%E4%BD%8D%E6%95%B0%E4%B8%8D%E4%B8%BA0%EF%BC%9B"><span class="nav-number">1.0.0.2.</span> <span class="nav-text">总结：若是这种写法DecimalFormat df &#x3D; new DecimalFormat(“#.##”)，则保留小数点后面不为0的两位小数，这种写法不能保证保留2为小数，但能保证最后一位数不为0；</span></a></li></ol></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E6%96%B9%E6%B3%95%E4%BA%8C%EF%BC%9A%E4%BD%BF%E7%94%A8String-format-%E6%96%B9%E6%B3%95"><span class="nav-number">2.</span> <span class="nav-text">方法二：使用String.format()方法</span></a><ol class="nav-child"><li class="nav-item nav-level-6"><a class="nav-link" href="#%E6%B3%A8%E9%87%8A%EF%BC%9A%E5%85%B6%E4%B8%ADString-s-x3D-String-format-%E2%80%9C-2f%E2%80%9D-d-%E8%A1%A8%E7%A4%BA%E5%B0%8F%E6%95%B0%E7%82%B9%E5%90%8E%E4%BB%BB%E6%84%8F%E4%B8%A4%E4%BD%8D%E5%B0%8F%E6%95%B0%EF%BC%8C%E5%85%B6%E4%B8%AD2%E4%B8%BA%E8%A1%A8%E7%A4%BA%E4%B8%A4%E4%BD%8D%E5%B0%8F%E6%95%B0%EF%BC%8C%E8%8B%A5%E9%9C%80%E8%A6%81%E4%B8%89%E4%BD%8D%E5%B0%8F%E6%95%B0%EF%BC%8C%E6%8A%8A2%E6%94%B9%E4%B8%BA3%E5%8D%B3%E5%8F%AF%EF%BC%8C%E5%85%B6%E4%BB%96%E5%90%8C%E7%90%86%E3%80%82"><span class="nav-number">2.0.0.0.0.1.</span> <span class="nav-text">注释：其中String s&#x3D;String.format(“%.2f”,d)表示小数点后任意两位小数，其中2为表示两位小数，若需要三位小数，把2改为3即可，其他同理。</span></a></li></ol></li></ol></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E6%80%BB%E7%BB%93%EF%BC%9A%E8%BF%99%E7%A7%8D%E6%96%B9%E6%B3%95%E4%B8%8D%E7%AE%A1%E4%BC%A0%E5%85%A5%E7%9A%84%E5%80%BC%E6%98%AF%E5%A4%9A%E5%B0%91%EF%BC%8C%E5%9D%87%E4%BF%9D%E7%95%99%E4%B8%A4%E4%BD%8D%E5%B0%8F%E6%95%B0%EF%BC%8C%E5%B9%B6%E4%B8%94%E7%AC%A6%E5%90%88%E5%9B%9B%E8%88%8D%E4%BA%94%E5%85%A5%E7%9A%84%E8%A7%84%E5%88%99%E3%80%82"><span class="nav-number">2.0.0.1.</span> <span class="nav-text">总结：这种方法不管传入的值是多少，均保留两位小数，并且符合四舍五入的规则。</span></a></li></ol></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E6%96%B9%E6%B3%95%E4%B8%89%EF%BC%9A%E4%BD%BF%E7%94%A8BigDecimal%E7%B1%BB"><span class="nav-number">3.</span> <span class="nav-text">方法三：使用BigDecimal类</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E6%80%BB%E7%BB%93%EF%BC%9A%E4%BD%BF%E7%94%A8%E8%BF%99%E7%A7%8D%E5%86%99%E6%B3%95%E8%8B%A5%E5%B0%8F%E6%95%B0%E7%82%B9%E5%90%8E%E5%9D%87%E4%B8%BA%E9%9B%B6%EF%BC%8C%E5%88%99%E4%BF%9D%E7%95%99%E4%B8%80%E4%BD%8D%E5%B0%8F%E6%95%B0%EF%BC%8C%E5%B9%B6%E4%B8%94%E6%9C%89%E5%9B%9B%E8%88%8D%E4%BA%94%E5%85%A5%E7%9A%84%E8%A7%84%E5%88%99%E3%80%82"><span class="nav-number">3.0.0.1.</span> <span class="nav-text">总结：使用这种写法若小数点后均为零，则保留一位小数，并且有四舍五入的规则。</span></a></li></ol></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E6%BA%90%E7%A0%81"><span class="nav-number">4.</span> <span class="nav-text">源码</span></a></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="John Doe"
      src="/images/avatar.jpg">
  <p class="site-author-name" itemprop="name">John Doe</p>
  <div class="site-description" itemprop="description">不积跬步，无以至千里；不积小流，无以成江海。</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">47</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">8</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">32</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2025</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">John Doe</span>
</div>

        








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/muse.js"></script>


<script src="/js/next-boot.js"></script>




  















  

  

</body>
</html>
